// 如果没有通过拦截器配置域名的话，可以在这里写上完整的URL(加上域名部分)
const honeBannerUrl = '/banner/json';
const topArticleUrl = '/article/top/json'
const projectTreeUrl = '/project/tree/json'
const wxarticleChaptersUrl = "/wxarticle/chapters/json"
const classifyListUrl = "/tree/json"
const naviUrl = "/navi/json"
const loginUrl = "/user/login"

// 此处第二个参数vm，就是我们在页面使用的this，你可以通过vm获取vuex等操作，更多内容详见uView对拦截器的介绍部分：

const install = (Vue, vm) => {

	// 由于uView-UI封装的网络层，显示loading只能在config全局配置
	// 所以这里做了请求拦截用于处理loading显示
	let loadingRequest = (url, method, data = {}, header = {}, showLoading = true) => {
		if (showLoading) {
			uni.showLoading({
				title: '加载中...'
			});
		}
		return vm.$u[method](url, data, header)
			.then(data => {
				return Promise.resolve(data);
			})
			.finally(
				() => {
					if (showLoading) {
						uni.hideLoading();
					}
				}
			);
	};

	// 获取首页轮轮播图
	let getHomeBanner = () => vm.$u.get(honeBannerUrl);

	// 获取首页置顶文章
	let getTopArticle = () => vm.$u.get(topArticleUrl);

	// 获取文章列表
	let getArticleList = (pageIndex) => vm.$u.get(`/article/list/${pageIndex}/json`);

	//获取项目分类 
	let getProjectTree = () => vm.$u.get(projectTreeUrl);

	// 获取项目列表
	let getProjectList = (projectId, pageIndex) => vm.$u.get(`/project/list/${pageIndex}/json?cid=${projectId}`);

	//获取项目分类
	let getWxarticleChapters = () => vm.$u.get(wxarticleChaptersUrl);

	// 获取项目列表
	let getWxarticleList = (wxarticleChaptersId, pageIndex) => vm.$u.get(
		`/wxarticle/list/${wxarticleChaptersId}/${pageIndex}/json`);

	// 获取分类列表
	let getClassifyList = () => vm.$u.get(classifyListUrl);

	//  获取导航
	let getNaviList = () => vm.$u.get(naviUrl);

	// 用户登录
	let userLogin = (params = {}) => loadingRequest(loginUrl, "post", params);

	// 收藏文章 
	let collectArticle = (articleID)=>vm.$u.post(`/lg/collect/${articleID}/json`);
	
	// 取消收藏文章
	let uncollectArticle = (articleID)=>vm.$u.post(`/lg/uncollect_originId/${articleID}/json`);

	// 将各个定义的接口名称，统一放进对象挂载到vm.$u.api(因为vm就是this，也即this.$u.api)下
	vm.$u.api = {
		getHomeBanner,
		getTopArticle,
		getArticleList,
		getProjectTree,
		getProjectList,
		getWxarticleChapters,
		getWxarticleList,
		getClassifyList,
		getNaviList,
		userLogin,
		collectArticle,
		uncollectArticle
	};
}

export default {
	install
}
